// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'onsenui/css/onsenui.css'
import 'onsenui/css/onsen-css-components.css'

import Vue from 'vue'
import VueOnsen from 'vue-onsenui'
import VueCordova from 'vue-cordova'
import VConsole from 'vconsole'
import { MulticastDNS } from './lib/mdns'

import store from './store'
import App from './App'

Vue.config.productionTip = true // false

if (Vue.config.productionTip) {
  Vue.use(new VConsole())
}

Vue.use(VueCordova)
Vue.use(VueOnsen)

Vue.cordova.on('deviceready', () => {
  // here check for your variable
  console.log('Cordova : device is ready !')

  console.log('Vue.cordova.plugins: ', Vue.cordova.plugins)
  console.log('window Cordova plugins: ', window.cordova.plugins)
  // console.log('window chrome: ', window.chrome)

  let mDNS = MulticastDNS()

  mDNS.on('error', function (err) {
    console.error('mdns error:', JSON.stringify(err))
  })

  mDNS.on('warning', function (err) {
    console.warn('mdns warning:', JSON.stringify(err))
  })

  mDNS.on('response', function (response) {
    console.log('got a response packet:', response)
  })

  mDNS.on('query', function (query) {
    console.log('got a query packet:', query)
    // iterate over all questions to check if we should respond
    query.questions.forEach(function (q) {
      if (q.type === 'A' && q.name === 'example.local') {
        // send an A-record response for example.local
        mDNS.respond({
          answers: [{
            name: 'example.local',
            type: 'A',
            ttl: 300,
            data: '192.168.1.5'
          }]
        })
      }
    })
  })

  // lets query for an A-record for example.local
  mDNS.on('ready', () => {
    mDNS.query({
      questions: [{
        name: '_http._tcp.local',
        type: 'A'
      }]
    }, (res) => {
      console.log(res)
    })
  })
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  template: '<App/>',
  components: { App }
})
